package com.erp.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.Utils.ResponseModel;
import com.erp.entity.Adminlog;
import com.erp.enums.statusCode;
import com.erp.service.AdminlogService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2021-10-21
 */
@Controller
@RequestMapping("/adminlog")
public class AdminlogController {
    @Resource
    AdminlogService adminlogService;

    @GetMapping("listView")
    public String listView(){
        return "system/log";
    }

    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page") int page,
                                    @RequestParam(value = "limit") int limit,
                                    @RequestParam(value = "userName",required = false) String username){
        IPage<Adminlog> adminlogIPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        QueryWrapper<Adminlog> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(username)){
            wrapper.like("userName",username);
        }
        wrapper.eq("status", statusCode.Default.getValue()).orderByDesc("createtime");
        adminlogIPage = adminlogService.page(adminlogIPage,wrapper);
        List<Adminlog> adminlogList = adminlogIPage.getRecords();//获取数据，这里面还有当前页码数还有总数据参数
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", adminlogIPage.getTotal());
        map.put("data",adminlogList);
        return map;
    }

    @DeleteMapping("delete")
    @ResponseBody
    public ResponseModel delete(){

        boolean result = adminlogService.update(null,new UpdateWrapper<Adminlog>().set("status",statusCode.Delete.getValue()));
        if (result){
            return ResponseModel.success("清空成功");
        }else{
            return ResponseModel.fail("清空失败");
        }
    }



}
